package org.example.testdemo.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import org.example.testdemo.pojo.Authorize;
import org.example.testdemo.pojo.PageReq;
import org.example.testdemo.pojo.Project;
import org.example.testdemo.service.AuthorizeService;
import org.example.testdemo.service.ProjectService;
import org.example.testdemo.tool.ApiResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController()
@RequestMapping("/authorize")
public class AuthorizeController {

    @Resource
    private AuthorizeService authorizeService;

    @Resource
    private ProjectService projectService;

    @PostMapping("/getAuthorizeInformation")
    public ApiResponse findByProjectId(String name, @NotNull(message = "参数异常") String pId, PageReq pageReq){
        if (null == name || name.isEmpty()) {
            name = null;
        }
        return ApiResponse.success(authorizeService.findByProjectId(name,pId,pageReq));
    }

    @PostMapping("addAuthorize")
    public ApiResponse addAuthorize(@Validated Authorize authorize, HttpServletRequest request){
        //查询此项目归属
        Project project = projectService.querySubordinateProjects(authorize.getPId());
        if(project==null){
            return ApiResponse.fail(20000,"此项目无效");
        }
        //获取用户id
        String userId = (String) request.getSession().getAttribute("userId");
        //判断是否能够修改
        if(!userId.equals(project.getUserId())){
            return ApiResponse.fail(20000,"没有权限操作此项目");
        }
        authorize.setBCode(project.getBCode());
        authorize.setCreationTime(LocalDateTime.now());
        Integer authorize1 = authorizeService.addAuthorize(authorize);
        if(authorize1<1){
            return ApiResponse.fail(20000,"新增授权信息失败");
        }
        return ApiResponse.success();
    }

    @PostMapping("deleteAuthorize")
    public ApiResponse deleteAuthorize(@NotNull(message = "参数异常") String authorizeId, HttpServletRequest request){
        //通过授权id查询授权信息
        Authorize authorize = authorizeService.getAuthorize(authorizeId);
        if(authorize==null){
            return ApiResponse.fail(20000,"没有找到授权信息");
        }
        //查询项目信息
        Project project = projectService.querySubordinateProjects(authorize.getPId());
        if(project==null){
            return ApiResponse.fail(20000,"此项目无效");
        }
        //获取用户id
        String userId = (String) request.getSession().getAttribute("userId");
        //判断是否能够修改
        if(!userId.equals(project.getUserId())){
            return ApiResponse.fail(20000,"没有权限操作此项目");
        }
        Integer authorize1 = authorizeService.deleteAuthorize(authorizeId);
        if(authorize1<1){
            return ApiResponse.fail(20000,"移除授权失败");
        }
        return ApiResponse.success();
    }

}
